<?php

namespace Admin\Controller;

class PostController extends BaseController {

    public function _initialize() {
        $this->module = '文章';
        $this->module_en = 'Post';
        parent::_initialize();
    }

    /**
     * 文章列表
     */
    public function lists() {
        $page = I('request.page');
        $page_size = \Think\Constant::PAGESIZE;
        $this->model = D('Post')->view('list')->page($page, $page_size)->limit($page_size)->order('post_id desc');
        $this->assign('cur_crumb', array(array('name' => '文章列表')));
        $this->assign('page_title', '文章列表');
        $this->assign('action_link', array(array('name' => '文章添加', 'link' => U('Post/add'))));
        parent::lists();
    }

    public function ajaxList($map = array(), $order = 'post_id desc') {
        $model = D('Post')->view('list');
        $page = I('page', 1, 'intval');
        list($sort_field, $sort_type) = explode(' ', $order);
        $this->assign('sort_field', $sort_field)->assign('sort_type', $sort_type);
        $this->assign('list', $model->page($page, 5)->limit(5)->order($order)->loadList($map));
        $this->assign('ajax', 1);
        $content = $this->fetch('_list');
        return $content;
    }

    /**
     * 添加
     */
    public function add() {
        $this->assign('itemList', D('Item')->order('item_id desc')->loadList()); //分类列表
        $this->assign('tagList', D('Tag')->loadList());   //标签列表
        $this->assign('typeList');
        $this->assign('cur_crumb', array(array('name' => '文章添加')));
        $this->assign('page_title', '撰写新文章');
        $this->assign('action_link', array(array('name' => '文章列表', 'link' => U('Post/lists'))));
        parent::add('edit');
    }

    public function edit() {
        $post_id = I('post_id', 0, 'intval');
        empty($post_id) && $this->error('页面不存在！');
        $this->assign('itemList', D('Item')->order('item_id desc')->loadList()); //分类列表
        $this->model = D('Post');
        $this->model = $this->model->view('info')->group('post_id');
        $this->query_condition = array('post_id' => $post_id);
        $this->assign('cur_crumb', array(array('name' => '文章编辑')));
        $this->assign('page_title', '文章编辑');
        $this->assign('action_link', array(array('name' => '文章列表', 'link' => U('Post/lists'))));
        parent::edit();
    }

    /**
     * 发布文章
     */
    public function post() {
        $model = D('Post');
        $res = $model->post(I('post.'));
        if ($res === false) {
            $this->error($model->getError());
        } else {
            redirect(U('Post/edit', array('post_id' => $res)));
        }
    }

    public function insert() {
        parent::insert();
    }

    /**
     * 搜索
     */
    public function search() {
        $post = I('post.');
        if ($post['sort_field']) {
            $order = $post['sort_field'] . ' ' . getSortType($post['sort_type'], isset($post['sort_reverse']) ? false : true);
        }
        $content = $this->ajaxList(isset($map) ? $map : array(), $order);
        $this->ajaxReturn(array('info' => '查询成功', 'status' => 1, 'content' => $content));
    }

    /**
     * 删除
     */
    public function delete() {
        
    }

    /**
     * 移入回收站
     */
    public function del() {
        
    }

}
